package com.projects.android.bus;

import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import com.projects.android.dto.CityDTO;
import com.projects.android.utils.Utils;

public class CityBUS {
	public static ArrayList<CityDTO> selectAll() {
		ArrayList<CityDTO> myData = null;
		try {
			String url = Utils.SERVICE_URI + "/City_SelectAll";
			// Send GET request to <service>/GetPlates
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(new URI(url));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("CityBUS selectAll", "Response: " + result);

			if (result != "") {
				// Init array objects
				JSONArray arrData = new JSONArray(result);
				myData = new ArrayList<CityDTO>();
				// Loop for all reveived objects
				for (int i = 0; i < arrData.length(); i++) {
					JSONObject data = (JSONObject) arrData.get(i);
					CityDTO dto = new CityDTO();
					dto.initObject(data);
					myData.add(dto);
				}
			}
		} catch (Exception e) {
			Log.e("CityBUS selectAll", e.toString());
		}
		return myData;
	}
}
